//
//  VoucherViewController.m
//  jungoo
//
//  Created by Dang Quang Trung on 8/25/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "VoucherViewController.h"

@implementation NSDictionary (Extension)

- (id)newObjectForKey:(id)aKey
{
    id rez = [self objectForKey:aKey];
    
    return ([rez isKindOfClass:[NSNull class]] ? nil : rez);
}
@end
@implementation VoucherViewController

@synthesize webView,lblTimer,lblOfferID,pgControl,data;

int hours, minutes, seconds;
int secondsLeft;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
-(void)dealloc
{
    [webView release];
    [lblTimer release];
    [lblOfferID release];
    [data release];
    [pgControl release];
    [super dealloc];
}

#pragma mark - Action 
-(IBAction)DonePressed:(id)sender
{
    if ([timer isValid]) {
        [timer invalidate];
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)updateCounter:(NSTimer *)theTimer {
    
    if(secondsLeft > 0 ){
        int redundancy = secondsLeft % 5;
        switch (redundancy) {
            case 0:
                [pgControl setCurrentPage:0];
                break;
            case 1:
                [pgControl setCurrentPage:4];
                break;
            case 2:
                [pgControl setCurrentPage:3];
                break;
            case 3:
                [pgControl setCurrentPage:2];
                break;
            case 4:
                [pgControl setCurrentPage:1];
                break;
            default:
                break;
        }
        secondsLeft -- ;
        hours = secondsLeft / 3600;
        minutes = (secondsLeft % 3600) / 60;
        seconds = (secondsLeft %3600) % 60;
        lblTimer.text = [NSString stringWithFormat:@"%02d",minutes];
        
        
    }
    else{
        [timer invalidate];
        // do DonePressed action 
        [self.navigationController popViewControllerAnimated:YES];
        
    }
}

-(void)countdownTimer{
    hours = minutes  = 0;
    if([timer isValid])
    {
        [timer release];
    }
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
    [pool release];
} 
-(void)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - View lifecycle

-(void)initControlls
{
    // info label
    NSString *content = [NSString stringWithFormat:@"<span style=\'color: #00AFEB;\'>-%@ OFF</span> <span style=\'color: #AAAAAA;\'>%@ at %@. </span> ",[data newObjectForKey:@"discount"],[data newObjectForKey:@"title"],[data newObjectForKey:@"shop_name"]]; 
    [webView loadHTMLString:content baseURL:nil];
    [webView sizeToFit];
    
    webView.backgroundColor = [UIColor clearColor];
    webView.opaque = NO;
    
    //offerID label
    lblOfferID.text = [data newObjectForKey:@"id"];
    
    secondsLeft = 15*60;
    [self countdownTimer];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initControlls];
    [self.navigationItem setHidesBackButton:NO];
    
    [self setBackButtonImageActiveName:nil 
                          inactiveName:@"back-button" 
                                target:self 
                                action:@selector(back:)];
    
    [self setTitle:@"Voucher"];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"home_bg.png"]]];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
